﻿
namespace SolutionFramework.Service
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.ServiceModel.DomainServices.Hosting;
    using System.ServiceModel.DomainServices.Server;
    using HydraShellServer;
    using AbstraX.ServerInterfaces;
    using AbstraX.Contracts;
    using HydraShellServer.Contracts;
    using SolutionFramework.Entities;
    using System.Data.Objects.DataClasses;
    using System.ServiceModel.DomainServices.EntityFramework;
    using Hydra.Shell;
    using System.Diagnostics;
    using System.Data;

    [EnableClientAccess()]
    public class HydraPackageService : LinqToEntitiesDomainService<SolutionFrameworkEntities>, IAbstraXService, IHydraPackageService
    {
        public IDomainHostApplication DomainServiceHostApplication { get; set; }

        protected IHydraPackageHost PackageHost
        {
            get
            {
                return (IHydraPackageHost)this.DomainServiceHostApplication;
            }
        }

        IQueryable<T> IHydraPackageService.GetPackages<T>()
        {
            return this.ObjectContext.HydraPackages.Cast<T>();
        }

        IQueryable<T> IHydraPackageService.GetEditors<T>()
        {
            return this.ObjectContext.HydraEditors.Cast<T>();
        }

        IQueryable<T> IHydraPackageService.GetEditorExtensionsForEditor<T>(Guid EditorID)
        {
            return this.ObjectContext.HydraEditorExtensions.Where(e => e.EditorID == EditorID).Cast<T>();
        }

        IQueryable<T> IHydraPackageService.GetProtocolHandlers<T>()
        {
            return this.ObjectContext.ProtocolHandlers.Cast<T>();
        }

        [Query]
        public IQueryable<HydraEditorListItem> GetEditorList()
        {
            this.PackageHost.HydraPackageService = this;

            return this.PackageHost.EditorList.AsQueryable();
        }

        [Query]
        public IQueryable<HydraPackageListItem> GetPackageList()
        {
            this.PackageHost.HydraPackageService = this;

            return this.PackageHost.PackageList.AsQueryable();
        }

        [Query]
        public IQueryable<ProtocolHandlerListItem> GetProtocolHandlerList()
        {
            this.PackageHost.HydraPackageService = this;

            return this.PackageHost.ProtocolHandlersList.AsQueryable();
        }

        [Query]
        public IQueryable<HydraPackage> GetPackages()
        {
            return this.ObjectContext.HydraPackages;
        }

        [Query(IsDefault = true)]
        public IQueryable<HydraService> GetServices()
        {
            return this.ObjectContext.HydraServices;
        }

        [Insert]
        public void InsertHydraPackage(HydraPackage package)
        {
            if ((package.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(package, EntityState.Added);
            }
            else
            {
                this.ObjectContext.HydraPackages.AddObject(package);
            }

            this.ObjectContext.SaveChanges();
        }

        [Insert]
        public void InsertHydraService(HydraService service)
        {
            if ((service.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(service, EntityState.Added);
            }
            else
            {
                this.ObjectContext.HydraServices.AddObject(service);
            }

            this.ObjectContext.SaveChanges();
        }

        [Delete]
        public void DeletePackageList(HydraPackageListItem listItem)
        {
        }

        [Update]
        public void UpdatePackageList(HydraPackageListItem listItem)
        {
        }

        [Insert]
        public void InsertPackageList(HydraPackageListItem listItem)
        {
        }
    }
}


